package com.woody.led;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * @author xinchao.zhang
 * 
 */
public class MainApplication extends SingleFrameApplication {

	private JFrame mainFrame;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jdesktop.application.Application#startup()
	 */
	@Override
	protected void startup() {
		mainFrame = getMainFrame();
		mainFrame.setJMenuBar(createMenuBar());
		mainFrame.add(createToolBar(), BorderLayout.NORTH);
		mainFrame.add(createMainPanel(), BorderLayout.CENTER);
//		statusBar statusBar = new StatusBar(this, getContext().getTaskMonitor());
//		view.setStatusBar(statusBar);
		show(mainFrame);
	}

	private Component createMainPanel() {
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);

		splitPane.setOneTouchExpandable(true);
		splitPane.setName("main_split_pane");

		JComponent programPanel = createProgramPanel();
		JComponent editPanel = createEditPanel();

		splitPane.add(programPanel);
		splitPane.add(editPanel);
		splitPane.setDividerLocation(200);

		return splitPane;
	}

	private JComponent createEditPanel() {
		// create tab  
		JTabbedPane tabbedpane = new JTabbedPane(); 
	  
		URL url = MainApplication.class.getResource("/images/Program1.png");
		ImageIcon icon = new ImageIcon(url);
	 	JLabel pix = new JLabel(icon); 
	 	tabbedpane.add("Program 1", pix); 
	  
	 	url = MainApplication.class.getResource("/images/Program2.png");
		icon = new ImageIcon(url);
	 	pix = new JLabel(icon); 
	 	tabbedpane.add("Program 2", pix); 
	  
	 	pix = new JLabel(); 
	 	tabbedpane.add("Program 3", pix); 
	 
	 	pix = new JLabel(); 
	 	tabbedpane.add("Program 4", pix); 
	 	
		return tabbedpane;
	}

	private JComponent createProgramPanel() {
		DefaultMutableTreeNode top = new DefaultMutableTreeNode("TOP");
		DefaultMutableTreeNode childe1 = new DefaultMutableTreeNode("Program 1");
		DefaultMutableTreeNode childe2 = new DefaultMutableTreeNode("Program 2");
		DefaultMutableTreeNode childe3 = new DefaultMutableTreeNode("Program 3");
		DefaultMutableTreeNode childe4 = new DefaultMutableTreeNode("Program 4");
		top.add(childe1);
		top.add(childe2);
		top.add(childe3);
		top.add(childe4);

		@SuppressWarnings("serial")
		JTree tree = new JTree(top) {
			public Insets getInsets() {
				return new Insets(5, 5, 5, 5);
			}
		};

		tree.setEditable(true);

		return new JScrollPane(tree);
	}

	private Component createToolBar() {
		String[] toolbarActionNames = { "newAD", "fileOpen", "fileSave" };
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.setRollover(true);

		JButton button = new JButton(getAction("search"));
		button.setPreferredSize(new Dimension(65, 65));
		toolBar.add(button);

		JComboBox comboBox = new JComboBox(toolbarActionNames);
		comboBox.setRenderer(new ComplexCellRenderer());
		comboBox.setPreferredSize(new Dimension(85, 65));
		toolBar.add(comboBox);

		JButton button2 = new JButton(getAction("run"));
		button2.setPreferredSize(new Dimension(65, 65));
		toolBar.add(button2);

		JButton button3 = new JButton(getAction("stop"));
		button3.setPreferredSize(new Dimension(65, 65));
		toolBar.add(button3);

		return toolBar;
	}

	private JMenuBar createMenuBar() {
		String[] fileMenuActionNames = { "newAD", "fileOpen", "fileSave", "-",
				"quit" };
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(createMenu("File", fileMenuActionNames));
		return menuBar;
	}

	@Action
	public void newAD() {
		System.out.println("New file...");
	}

	@Action
	public void fileOpen() {
		System.out.println("Open file...");
	}

	@Action
	public void fileSave() {
		System.out.println("Save");
	}

	@Action
	public void search() {

	}

	@Action
	public void run() {

	}

	@Action
	public void stop() {

	}

	private JMenu createMenu(String menuName, String[] actionNames) {
		JMenu menu = new JMenu();
		menu.setName(menuName);
		for (String actionName : actionNames) {
			if (actionName.equals("-")) {
				menu.addSeparator();
			} else {
				JMenuItem menuItem = new JMenuItem();
				menuItem.setAction(getAction(actionName));
				menu.add(menuItem);
			}
		}
		return menu;

	}

	private javax.swing.Action getAction(String actionName) {
		return getContext().getActionMap().get(actionName);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Application.launch(MainApplication.class, args);
	}
}
